package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.CrossZhenHuanPromotionData;
import com.yanqu.road.entity.activity.zhenhuan.ZhenHuanPromotionInfo;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanPartyImpl extends TempDataObjectDao<CrossZhenHuanPartyData> {
    public static String getCrossZhenHuanPartyDataSql = "select * from `t_c_zhenhuan_party` where `activityId`=?";
    public Map<Long, CrossZhenHuanPartyData> getCrossZhenHuanPartyData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossZhenHuanPartyData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossZhenHuanPartyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanPartyData data = parse(rs);
                dataMap.put(data.getPartyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossZhenHuanPartyDataSql = "update `t_c_zhenhuan_party` set userId=?,openTime=?,finishTime=?,endTime=?,level=?,extraPrestige=?,syncState=? where activityId=? and partyId=?";
    @Override
    protected boolean updateData(CrossZhenHuanPartyData crossZhenHuanPartyData) {
        crossZhenHuanPartyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getOpenTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getExtraPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getSyncState()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getPartyId()));
        if (getDb().executeNoneQuery(updateCrossZhenHuanPartyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossZhenHuanPartyDataSql = "insert into `t_c_zhenhuan_party`(activityId,partyId,userId,openTime,finishTime,endTime,level,extraPrestige,syncState) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossZhenHuanPartyData crossZhenHuanPartyData) {
        crossZhenHuanPartyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getPartyId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getOpenTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyData.getExtraPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyData.getSyncState()));
        if (getDb().executeNoneQuery(insertCrossZhenHuanPartyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossZhenHuanPartyData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanPartyData crossZhenHuanPartyData = new CrossZhenHuanPartyData();
        crossZhenHuanPartyData.setActivityId(rs.getInt("activityId"));
        crossZhenHuanPartyData.setPartyId(rs.getLong("partyId"));
        crossZhenHuanPartyData.setUserId(rs.getLong("userId"));
        crossZhenHuanPartyData.setOpenTime(rs.getLong("openTime"));
        crossZhenHuanPartyData.setFinishTime(rs.getLong("finishTime"));
        crossZhenHuanPartyData.setEndTime(rs.getLong("endTime"));
        crossZhenHuanPartyData.setLevel(rs.getInt("level"));
        crossZhenHuanPartyData.setExtraPrestige(rs.getLong("extraPrestige"));
        crossZhenHuanPartyData.setSyncState(rs.getInt("syncState"));

        return crossZhenHuanPartyData;
    }
}
